package cn.manjuu.touhou.engine;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Handler;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.activity.fragment.MakeMoneyFragment;
import cn.manjuu.touhou.interfaces.OnBaseInfoChangeListener;
import cn.manjuu.touhou.util.Looger;

/**
 * 用来控制战斗逻辑的
 * 
 * @author 宋疆疆
 * @since 2013.5.22
 */
public class BattleEngine {

	private static final int COUNT_DOWN = 0;
	public static final int NULL = -1;
	public static final int ATTACK = 0;
	public static final int DEFEND = 1;
	public static final int RECOVER = 2;
	private static BattleEngine mInstance;
	private ScheduledExecutorService scheduledExecutorService;
	private MakeMoneyFragment mFragment;
	private int playerState;
	private int enemyState;
	private OnBaseInfoChangeListener mListener;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case COUNT_DOWN:
				countDownTime();
				break;
			}
		}

	};
	private Random random;

	public static BattleEngine getInstance() {
		if (null == mInstance) {
			mInstance = new BattleEngine();
		}
		return mInstance;
	}

	private BattleEngine() {
		super();
		random = new Random();
	}

	public void initState() {

		playerState = NULL;
		enemyState = NULL;
	}

	public void startBattle() {
		initPb();
		initState();
		scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		scheduledExecutorService.scheduleWithFixedDelay(new coundDownTask(), 0,
				50, TimeUnit.MILLISECONDS);
	}

	private void initPb() {
		// TODO Auto-generated method stub
		mFragment.getPb_enemy_hp().setProgress(
				mFragment.getPb_enemy_hp().getMax() - 5);
		mFragment.getPb_player_hp().setProgress(
				mFragment.getPb_player_hp().getMax() - 5);
		mFragment.getPb_left_time().setProgress(
				mFragment.getPb_left_time().getMax());
	}

	private class coundDownTask implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			mHandler.sendEmptyMessage(COUNT_DOWN);
		}

	}

	public void shutdown() {
		if (null != scheduledExecutorService) {
			scheduledExecutorService.shutdown();
			scheduledExecutorService = null;
		}
	}

	private void countDownTime() {
		// TODO Auto-generated method stub
		if (0 == mFragment.getPb_left_time().getProgress()) {
			checkWinner();
			mFragment.getPb_left_time().setProgress(60);
		} else if (20 == mFragment.getPb_left_time().getProgress()) {
			enemyState = random.nextInt(3);
			mFragment.getPb_left_time().setProgress(
					mFragment.getPb_left_time().getProgress() - 1);
		} else {
			mFragment.getPb_left_time().setProgress(
					mFragment.getPb_left_time().getProgress() - 1);
		}
	}

	/**
	 * 根据玩家和敌人的状态判定输赢
	 */
	private void checkWinner() {
		// TODO Auto-generated method stub
		Looger.i(APP.BattleEngine, "playerState: " + playerState
				+ ", enemyState: " + enemyState);
		switch (playerState) {
		case NULL:
			switch (enemyState) {
			case ATTACK:
				playerChangeHp(-2);
				break;
			case DEFEND:
				break;
			case RECOVER:
				enemyChangeHp(2);
				break;
			}
			break;
		case ATTACK:
			switch (enemyState) {
			case ATTACK:
				enemyChangeHp(-2);
				playerChangeHp(-2);
				break;
			case DEFEND:
				enemyChangeHp(-1);
				break;
			case RECOVER:
				enemyChangeHp(-3);
				break;
			}
			break;
		case DEFEND:
			switch (enemyState) {
			case ATTACK:
				playerChangeHp(-1);
				break;
			case DEFEND:
				break;
			case RECOVER:
				enemyChangeHp(2);
				break;
			}

			break;
		case RECOVER:
			switch (enemyState) {
			case ATTACK:
				playerChangeHp(-3);
				break;
			case DEFEND:
				playerChangeHp(-1);
				break;
			case RECOVER:
				enemyChangeHp(2);
				playerChangeHp(2);
				break;
			}
			break;
		}
		initState();
	}

	private void playerChangeHp(int i) {
		// TODO Auto-generated method stub
		int hp = mFragment.getPb_player_hp().getProgress() + i;
		if (hp <= 0) {
			playerDead();
		}
		mFragment.getPb_player_hp().setProgress(hp);
	}

	private void playerDead() {
		// TODO Auto-generated method stub
		BaseInfoEngine.getInstance().reduceMoney(50);
		mListener.onBaseInfoChangeListener();
		NavigationEngine.getInstance().toToyMachineFragment();
		NavigationEngine.getInstance().toDaramActivity("player_lose");
	}

	private void enemyChangeHp(int i) {
		// TODO Auto-generated method stub
		int hp = mFragment.getPb_enemy_hp().getProgress() + i;
		if (hp <= 0) {
			enemyDead();
		}
		mFragment.getPb_enemy_hp().setProgress(hp);
	}

	private void enemyDead() {
		// TODO Auto-generated method stub
		BaseInfoEngine.getInstance().increaseMoney(50);
		mListener.onBaseInfoChangeListener();
		NavigationEngine.getInstance().toToyMachineFragment();
		NavigationEngine.getInstance().toDaramActivity("player_win");
	}

	public void setFragment(MakeMoneyFragment makeMoneyFragment) {
		// TODO Auto-generated method stub
		mFragment = makeMoneyFragment;
	}

	public void changePlayerState(int state) {
		playerState = state;
	}

	public void setListener(OnBaseInfoChangeListener listener) {
		// TODO Auto-generated method stub
		mListener = listener;
	}
}
